package com.example.demo.service.impl;

import com.example.demo.service.ThreadTestService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Service
public class ThreadTestServiceImp implements ThreadTestService {

    @Override
    @Async(value = "doSomethingExecutor") //声明线程任务
    public void threadTask(int i) {
        System.out.println(Thread.currentThread().getName() + "线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("线程:" + Thread.currentThread().getName() + ":" + i);
    }

    @Override
    @Async(value = "doSomethingExecutor") //声明线程任务
    public CompletableFuture<String> threadTaskWithReturn01() {
        System.out.println(Thread.currentThread().getName() + "线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CompletableFuture.completedFuture(Thread.currentThread().getName() + ":threadTaskWithReturn01()执行");
    }

    @Override
    @Async(value = "doSomethingExecutor") //声明线程任务
    public CompletableFuture<String> threadTaskWithReturn02() {
        System.out.println(Thread.currentThread().getName() + "线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CompletableFuture.completedFuture(Thread.currentThread().getName() + ":threadTaskWithReturn02()执行");
    }

    @Override
    @Async(value = "doSomethingExecutor") //声明线程任务
    public CompletableFuture<String> threadTaskWithReturn03() {
        System.out.println(Thread.currentThread().getName() + "线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CompletableFuture.completedFuture(Thread.currentThread().getName() + ":threadTaskWithReturn03()执行");
    }

    @Override
    @Async(value = "doSomethingExecutor") //声明线程任务
    public CompletableFuture<String> threadTaskWithReturn04() {
        System.out.println(Thread.currentThread().getName() + "线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CompletableFuture.completedFuture(Thread.currentThread().getName() + ":threadTaskWithReturn04()执行");
    }

}
